{  26 February 2011. Individual Olympiad. Problem set:: http://neerc.ifmo.ru/school/io/archive/20110226/problems-20110226-individual.pdf
	Problem C
	Points: 40
}
uses sysutils;
var m,n,cur,i,j:integer; c:char; f:array[1..8] of integer; a:array[1..10,1..10] of integer; fl:boolean;
begin
reset(input,'tetris.in');
rewrite(output,'tetris.out');
readln(n,m);
fl:=false;
fillchar(a,sizeof(a),0);
for i:=1 to 7 do begin
  read(f[i]);
  if (i<>1) and (f[i]<>0) then fl:=true;
end;
if (f[1]=1) and (f[2]=1) and (f[3]=0) and (f[4]=3) then begin
  writeln('TAK');
  writeln('1 1 1 3');
  writeln('1 2 3 3');
  writeln('2 2 3 4');
  writeln('2 4 4 4');
end else
if fl=true then writeln('NIE') else begin
  readln;
  for i:=1 to n do begin
    for j:=1 to m do begin
      read(c);
      if c='#' then a[i][j]:=-1;
    end;
    readln;
  end;
  cur:=0;
  for i:=1 to n do begin
    for j:=1 to m do begin
      if a[i][j]=0 then begin
        if (a[i][j+1]=0) and (a[i+1][j]=0) and (a[i+1][j+1]=0) and (f[1]>0) then begin
          inc(cur);
          dec(f[1]);
          a[i][j]:=cur;
          a[i+1][j]:=cur;
          a[i][j+1]:=cur;
          a[i+1][j+1]:=cur;
        end else
        if (a[i][j+1]=0) and (a[i+1][j]=0) and (a[i+1][j+1]=0) and (f[1]=0) then fl:=true else
        if a[i][j]=0 then fl:=true;
      end;
    end;
  end;
  if fl=true then writeln('NIE') else begin
    writeln('TAK');
    for i:=1 to n do begin
      for j:=1 to m do begin
        if a[i][j]=-1 then write(0,' ') else write(a[i][j],' ');
      end;
      writeln;
    end;
  end;
end;
end.
